//获取应用实例
var app = getApp();
Page({
    data: {
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        isHide: false,
        wxcode:""
    },
    onShow:function(){
      var that = this;
      wx.request({
        url: app.globalData.url + "getUserUpdradeStatus",
        method: "GET",
        data: {
          userId: wx.getStorageSync("userId")
        },
        success: function (res) {
          var data = res.data.data;
          if (res.data.result == 1) {
            if (res.data.data.authStatus == undefined) {
              that.setData({
                status: 0,
              })
            } else {
              that.setData({
                status: res.data.data.authStatus,
                hisId: res.data.data.hisId,
              })
            }

          }
        }
      });
    },
    onLoad: function (options) {
      var that = this;
      //获取手机高度
      that.setData({
        navH: app.globalData.navHeight,
        type: options.type
      })
      
      //商城首页banner
      wx.request({
        url: app.globalData.url + "getAdvertisementListByType",
        method: "GET",
        data: {
          advertisingType: options.type
        },
        success: function (res) {
          var data = res.data.data;
          if (res.data.result == 1) {
            that.setData({
              mallBanner: data,
            })
          }
        }
      });
      
    },
    navBack: function () {
      var that = this;
      wx.navigateBack({
        
      })
    },
    //用户支付
    userUpgrade:function(){
      var that=this;
      var userLevel=0;
      if (that.data.type == 6) {
        userLevel=2;//PLUS
      } else if (that.data.type == 7) {
        userLevel = 3;//PLUS
      }
      var hisId = 0;
      if (that.data.type == 7) {
        hisId = that.data.hisId;
      }
      wx.request({
        url: app.globalData.url + 'userUpgrade',
        data: {
          userId: wx.getStorageSync("userId"),
          userLevel: userLevel,
          hisId: hisId,
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var resultData = res.data;
          var hisId = resultData.hisId;
          if (res.data.result != 1 && res.data.result != 9) {
            wx.showToast({
              title: "数据异常，请稍后重试",
              icon: 'none'
            })
            return;
          }
          wx.requestPayment({
            'timeStamp': resultData.timeStamp,
            'nonceStr': resultData.nonceString,
            'package': resultData.packageStr,
            'signType': resultData.signType,
            'paySign': resultData.sign,
            //支付成功
            success: function (res) {
              if ("requestPayment:ok" === res.errMsg) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 2000,
                  success: function () {

                  }
                })
                that.onShow();
              }
            },
            fail: function (res) {
              if ("requestPayment:fail cancel" === res.errMsg) {
                wx.showToast({
                  title: '取消支付',
                  icon: 'none',
                  duration: 2000,
                  success: function () {

                  }
                })
              } else {
                wx.showToast({
                  title: "支付失败",
                  icon: 'none'
                })
              }
            },
          })
        }
      })
    },
  shopApplyFor:function(){
    wx.navigateTo({
      url: '/pages/shopApplyFor/shopApplyFor',
    })
  }
})
